encode Country, gen(countryID)
encode Electiontype, gen(type)
drop if type!=2

rename Country country

gen eu15=0
replace eu15=1 if countryID==1
replace eu15=1 if countryID==2
replace eu15=1 if countryID==7
replace eu15=1 if countryID==9
replace eu15=1 if countryID==10
replace eu15=1 if countryID==11
replace eu15=1 if countryID==12
replace eu15=1 if countryID==14
replace eu15=1 if countryID==15
replace eu15=1 if countryID==18
replace eu15=1 if countryID==20
replace eu15=1 if countryID==22
replace eu15=1 if countryID==26
replace eu15=1 if countryID==27
replace eu15=1 if countryID==28

rename Year year
drop if year>2016


gen shock=0
replace shock=1 if year>2010

gen treat=0
replace treat=1 if countryID==22
replace treat=1 if countryID==14
replace treat=1 if countryID==15
replace treat=1 if countryID==12
replace treat=1 if countryID==26


rename VoterTurnout turnout


sort country year
egen id2=group(country)
sort id2 year
by id2: gen turnout_lag=turnout[_n-1]

gen election=countryID*year


merge 1:1 election using cpds_aggregates.dta

sort countryID year
egen id=group(countryID)
sort id year
by id: gen deficitl=deficit[_n-1]
by id: gen unempl=unemp[_n-1]
by id: gen debtl=debt[_n-1]


gen unemp_change=unemp-unempl
gen debt_change=debt-debtl

factor unemp_change debt_change realgdpgr
sumscale, f1(unemp_change debt_change realgdpgr)
rename Factor1_average misery
label var misery "Economic misery"


by id: gen miseryl=misery[_n-1]



***Table A8***
reg turnout i.treat##i.shock c.turnout_lag i.countryID c.year if year<2016 & year>2002 & type==2 & eu15==1
outreg2 using aggregate.doc, dec(2) replace
reg turnout i.treat##i.shock c.turnout_lag c.deficitl i.countryID c.year if year<2016 & year>2002 & type==2 & eu15==1
outreg2 using aggregate.doc, dec(2) append
reg turnout i.treat##i.shock c.turnout_lag c.unempl i.countryID c.year if year<2016 & year>2002 & type==2 & eu15==1
outreg2 using aggregate.doc, dec(2) append
reg turnout i.treat##i.shock c.turnout_lag c.debtl i.countryID c.year if year<2016 & year>2002 & type==2 & eu15==1
outreg2 using aggregate.doc, dec(2) append
reg turnout i.treat##i.shock c.turnout_lag c.miseryl i.countryID c.year if year<2016 & year>2002 & type==2 & eu15==1
outreg2 using aggregate.doc, dec(2) append


***Figure 1***
reg turnout i.treat##i.shock c.turnout_lag c.deficitl i.countryID c.year if year<2016 & year>2002 & type==2 & eu15==1

coefplot, keep(*.treat *.shock *.treat#*.shock) xline(0) ci(95 90)
